package com.yaolisheng.testMain;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author : yaols
 * Desc:
 * Company: 神州数码信息系统有限公司
 * Date: ${YEAR}/${MONTH}/${DAY}/${TIME}
 */
public class ThreadMainTest {

    public static void main(String[] args) throws InterruptedException {

        List<String> strings = new ArrayList<>();
        for (int i = 0; i < 1001; i++) {
            strings.add(">>>" + i);
        }

        List<String> strings1 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            strings1.add(i + "");
        }
        Map<String, List<String>> map = new Hashtable<>();

        // 1. 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        CompletableFuture.allOf(strings1.stream().map(key -> CompletableFuture.runAsync(() -> {
            List<String> asdf = execTest(strings, key);
            map.put(key, asdf);
            System.out.println(key + "_");
        }, executorService)).toArray(CompletableFuture[]::new)).join();
        executorService.shutdown();
        System.out.println(map.size());
    }


    public static List<String> execTest(List<String> list, String id) {
        List<String> stringList = new Vector<>();

        // 1. 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(500);
        // 2. 提交任务，并调用join()阻塞等待所有任务执行完成
        CompletableFuture.allOf(list.stream().map(key -> CompletableFuture.runAsync(() -> {
            // 睡眠一秒，模仿处理过程
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
            }
            System.out.println("结果" + key+">>>"+id);
            stringList.add(key + ">>>" + id);
        }, executorService)).toArray(CompletableFuture[]::new)).join();
        executorService.shutdown();
        return stringList;
    }
}

